/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view.individual;

import canteen.sys.model.entity.BillMaster;
import canteen.sys.model.entity.ItemMaster;
import canteen.sys.model.entity.ItemTypeMaster;
import canteen.sys.model.generic.HibernateDAOFactory;
import canteen.sys.view.ActionView;
import canteen.sys.view.ComboboxRecInfo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TNT
 */
public class AddBill extends javax.swing.JInternalFrame implements ActionView<BillMaster> {
    
    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultTableModel tbItemsModel;
    DefaultTableModel tbItemPickModel;
    List<ItemMaster> listItem;
    List<ItemMaster> listItemPick;

    /**
     * Creates new form AddBill
     */
    public AddBill() {
        initComponents();
        tbItemPickModel = (DefaultTableModel) tableItemPicked.getModel();
        tbItemsModel = (DefaultTableModel) tbItems.getModel();
        listItemPick = new ArrayList<>();
        clearTableItem();
        clearTableItemPick();
        showListItemType();
        Date date = new Date();
        calDate.setDate(date);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editorLayer = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        txtCreateDate = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtCoupenNo = new javax.swing.JTextField();
        calDate = new org.freixas.jcalendar.JCalendarCombo();
        calCoupenDate = new org.freixas.jcalendar.JCalendarCombo();
        txtNo1 = new javax.swing.JTextField();
        itemDetailLayer = new javax.swing.JLayeredPane();
        txtItemQty = new javax.swing.JTextField();
        txtItemRate = new javax.swing.JTextField();
        btAdd = new javax.swing.JButton();
        btRemove = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableItemPicked = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        cbItemType = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbItems = new javax.swing.JTable();
        txtItemName = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        actionLayer = new javax.swing.JLayeredPane();
        btSave = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        setClosable(true);

        editorLayer.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        editorLayer.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N

        jLabel1.setText("No");
        jLabel1.setBounds(10, 20, 30, 14);
        editorLayer.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtCreateDate.setEnabled(false);
        txtCreateDate.setBounds(100, 50, 170, 20);
        editorLayer.add(txtCreateDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Date");
        jLabel5.setBounds(10, 50, 50, 14);
        editorLayer.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Coupen No");
        jLabel6.setBounds(10, 80, 90, 14);
        editorLayer.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Coupen Date");
        jLabel7.setBounds(10, 110, 90, 14);
        editorLayer.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtCoupenNo.setBounds(100, 80, 170, 20);
        editorLayer.add(txtCoupenNo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        calDate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MM/dd/yyyy" }));
        calDate.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        calDate.setBounds(100, 190, 170, 20);
        editorLayer.add(calDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        calCoupenDate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MM/dd/yyyy" }));
        calCoupenDate.setDate(new java.util.Date(1348094944000L));
        calCoupenDate.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        calCoupenDate.setBounds(100, 110, 170, 20);
        editorLayer.add(calCoupenDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtNo1.setEnabled(false);
        txtNo1.setBounds(100, 20, 170, 20);
        editorLayer.add(txtNo1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        itemDetailLayer.setBorder(javax.swing.BorderFactory.createTitledBorder("Item Details"));
        txtItemQty.setBounds(230, 240, 40, 20);
        itemDetailLayer.add(txtItemQty, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtItemRate.setEditable(false);
        txtItemRate.setBounds(280, 240, 110, 20);
        itemDetailLayer.add(txtItemRate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btAdd.setText("Add");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });
        btAdd.setBounds(400, 210, 80, 23);
        itemDetailLayer.add(btAdd, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btRemove.setText("Remove");
        btRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoveActionPerformed(evt);
            }
        });
        btRemove.setBounds(400, 240, 80, 23);
        itemDetailLayer.add(btRemove, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tableItemPicked.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "No.", "ItemName", "Qty", "Rate", "Amount"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableItemPicked);

        jScrollPane1.setBounds(10, 270, 460, 150);
        itemDetailLayer.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setText("Item Name");
        jLabel9.setBounds(80, 220, 60, 14);
        itemDetailLayer.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel10.setText("Item Rate");
        jLabel10.setBounds(300, 220, 60, 14);
        itemDetailLayer.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtTotal.setEnabled(false);
        txtTotal.setBounds(370, 440, 110, 20);
        itemDetailLayer.add(txtTotal, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setText("Total");
        jLabel12.setBounds(310, 440, 50, 14);
        itemDetailLayer.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbItemType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbItemTypeItemStateChanged(evt);
            }
        });
        cbItemType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbItemTypeActionPerformed(evt);
            }
        });
        cbItemType.setBounds(70, 20, 170, 20);
        itemDetailLayer.add(cbItemType, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Item Type");
        jLabel2.setBounds(10, 20, 90, 20);
        itemDetailLayer.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tbItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tbItems);

        jScrollPane2.setBounds(12, 50, 460, 140);
        itemDetailLayer.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtItemName.setEnabled(false);
        txtItemName.setBounds(79, 240, 140, 20);
        itemDetailLayer.add(txtItemName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setText("Item Qty");
        jLabel11.setBounds(230, 220, 60, 14);
        itemDetailLayer.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        actionLayer.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btSave.setText("Save");
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });
        btSave.setBounds(600, 10, 80, 23);
        actionLayer.add(btSave, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btCancel.setText("Cancel");
        btCancel.setBounds(680, 10, 80, 23);
        actionLayer.add(btCancel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(actionLayer)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(editorLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itemDetailLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editorLayer, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                    .addComponent(itemDetailLayer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(actionLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        // TODO add your handling code here:
//        try {
//            int indexItem = tbItems.getSelectedRow();
//            //            int qty = Integer.parseInt(txtItemQty.getText());
//            //            if (qty <= 0) {
//                //                JOptionPane.showMessageDialog(this, "Quantity must >0",
//                    //                        "Warning", JOptionPane.WARNING_MESSAGE);
//                //                return;
//                //            }
//            addRow(getObject(indexItem), true);
//        } catch (NumberFormatException ex) {
//            JOptionPane.showMessageDialog(this, "Must input correct format! Integer",
//                "Warning", JOptionPane.WARNING_MESSAGE);
//        }
        ItemMaster itemMaster = listItem.get(1);
        itemMaster.setItemName("TNTuyen");
        itemMaster.setQuantity(9);
        String name = listItem.get(1).getItemName();
        
    }//GEN-LAST:event_btAddActionPerformed
    
    private void btRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveActionPerformed
        // TODO add your handling code here:
//        int indexItemPick = tableItemPicked.getSelectedRow();
//        deleteRow(indexItemPick);
    }//GEN-LAST:event_btRemoveActionPerformed
    
    private void cbItemTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbItemTypeItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cbItemTypeItemStateChanged
    
    private void cbItemTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbItemTypeActionPerformed
        // TODO add your handling code here:
        if (cbItemType.getItemCount() > 0) {
            
            ComboboxRecInfo<ItemTypeMaster> combobox =
                    (ComboboxRecInfo<ItemTypeMaster>) cbItemType.getSelectedItem();
            List<ItemMaster> listItemToShow = hibernateDAOFactory.getItemMasterDAO()
                    .findAllByItemTypeIdAndStatus(combobox.getKeyData().getItemTypeId(), true);
            if (listItem != null) {
                listItem.clear();
                clearTableItem();
            }
            listItem = listItemToShow;
            showListItem(listItemToShow);
        }
    }//GEN-LAST:event_cbItemTypeActionPerformed
    
    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane actionLayer;
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btRemove;
    private javax.swing.JButton btSave;
    private org.freixas.jcalendar.JCalendarCombo calCoupenDate;
    private org.freixas.jcalendar.JCalendarCombo calDate;
    private javax.swing.JComboBox cbItemType;
    private javax.swing.JLayeredPane editorLayer;
    private javax.swing.JLayeredPane itemDetailLayer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tableItemPicked;
    private javax.swing.JTable tbItems;
    private javax.swing.JTextField txtCoupenNo;
    private javax.swing.JTextField txtCreateDate;
    private javax.swing.JTextField txtItemName;
    private javax.swing.JTextField txtItemQty;
    private javax.swing.JTextField txtItemRate;
    private javax.swing.JTextField txtNo1;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    @Override
    public void deleteRow(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void updateRow(BillMaster row, int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void addRow(BillMaster row, boolean posion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void showList(List<BillMaster> list) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void showDetail(BillMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public BillMaster getObject(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void setObject(BillMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public boolean validObject(BillMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private void clearTableItem() {
        try {
            if (tbItems.getRowCount() > 0) {
                if (listItem != null) {
                    listItem.clear();
                }
                tbItemsModel.setDataVector(null,
                        new Object[]{"Item Name", "Quantity", "Rate"});
                
            }
        } catch (NullPointerException ex) {
        }
    }
    
    private void clearTableItemPick() {
        try {
            if (tableItemPicked.getRowCount() > 0) {
                if (listItemPick != null) {
                    listItemPick.clear();
                }
                tbItemPickModel.setDataVector(null,
                        new Object[]{"No", "Item Name", "Quantity", "Rate", "Amount"});
            }
        } catch (NullPointerException ex) {
        }
    }
    
    private void showListItemType() {
        List<ItemTypeMaster> listItemType = hibernateDAOFactory
                .getItemTypeMasterDAO().findAllByStatus(true);
        for (ItemTypeMaster itemTypeMaster : listItemType) {
            ComboboxRecInfo<ItemTypeMaster> combobox = new ComboboxRecInfo<>();
            combobox.setName(itemTypeMaster.getItemTypeName());
            combobox.setKeyData(itemTypeMaster);
            cbItemType.addItem(combobox);
        }
    }
    
    private void showListItem(List<ItemMaster> listItemToShow) {
        try {
            if (listItemToShow != null && listItemToShow.size() > 0) {
                for (ItemMaster itemMaster : listItemToShow) {
                    tbItemsModel.insertRow(0, new Object[]{itemMaster.getItemName(),
                                itemMaster.getQuantity(),
                                "$" + itemMaster.getItemRate()});
                }
            }
        } catch (NullPointerException ex) {
        }
    }
}
